﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MonthCalendar;
using Db4objects.Db4o;
using Db4objects.Db4o.Linq;
using System.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for CalendarControl.xaml
	/// </summary>
	public partial class CalendarControl : UserControl
	{
		public CalendarControl()
		{
			this.InitializeComponent();
		}

		private void AppointmentDblClicked(long Appointment_Id)
		{
			//MessageBox.Show("You double-clicked on appointment with ID = " + Appointment_Id, "Calendar Event", MessageBoxButton.OK);
            AddAppointmentWindow aaw = new AddAppointmentWindow(Appointment_Id);
            aaw.ShowDialog();
            AppointmentDB adb = AppointmentDB.Instance;
            SetAppointments(adb);
		}

		private void DayBoxDoubleClicked_event(MonthCalendar.NewAppointmentEventArgs e)
		{
            //MessageBox.Show("You double-clicked on day " + Convert.ToDateTime(e.StartDate).ToShortDateString(), "Calendar Event", MessageBoxButton.OK);
            AddAppointmentWindow aaw = new AddAppointmentWindow(Convert.ToDateTime(e.StartDate));
            AppointmentDB adb = AppointmentDB.Instance;
            aaw.ShowDialog();
			SetAppointments(adb);
		}

		private void DisplayMonthChanged(MonthCalendar.MonthChangedEventArgs e)
		{
            AppointmentDB adb = AppointmentDB.Instance;
            SetAppointments(adb);
		}
		public void SetAppointments(AppointmentDB adb)
        {
            //-- Use whatever function you want to load the MonthAppointments list, I happen to have a list filled by linq that has
            //   many (possibly the past several years) of them loaded, so i filter to only pass the ones showing up in the displayed
            //   month.  Note that the "setter" for MonthAppointments also triggers a redraw of the display.

            //Okay Guys, I implemented a generic Linq query to pull entries from _myAppointmentsList. We just tell it somewher to store everything in a db4o database and poof! We're badass!
            IEnumerable<Appointment> aptEnum = from Appointment apt in adb.db
                                               where apt.StartTime != null && Convert.ToDateTime(apt.StartTime).Month == this.AptCalendar.DisplayStartDate.Month && Convert.ToDateTime(apt.StartTime).Year == this.AptCalendar.DisplayStartDate.Year
                                               select apt;
            AptCalendar.MonthAppointments = aptEnum.ToList<Appointment>();
            //Below is the old query that was originally used. I didn't understand it, so I converted it to a more understandable one above.^^
            //AptCalendar.MonthAppointments = _myAppointmentsList.FindAll(new System.Predicate<Appointment>((Appointment apt) => apt.StartTime != null && Convert.ToDateTime(apt.StartTime).Month == this.AptCalendar.DisplayStartDate.Month && Convert.ToDateTime(apt.StartTime).Year == this.AptCalendar.DisplayStartDate.Year));
        }
	}
}